
<!doctype html>
<html class="no-js" lang="">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Untitled Document</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="apple-touch-icon" href="apple-touch-icon.png">
    <!-- Place favicon.ico in the root directory -->

    <link rel="stylesheet" type="text/css" href="../../css/reset.css">
</head>

<body>
    <div class="loading">
        <span class="loadImg"></span>
    </div>
    <div class="bannerList">
        <div class="info banner">
            <ul>
                <li class="top">
                    <span class="id">id</span>
                    <span class="name">名称</span>
                    <span class="logo">图片</span>
                    <span class="title">标题</span>
                    <span class="url">链接</span>
                    <span class="texta">文本</span>
                    <span class="click">操作</span>
                </li>
            </ul>
        </div>
    </div>
    <div class="revise">
        <div class="info inputList">
            <p class="li"><span>名称11：</span><input type="text" class="t1 mel" name=""></p>
            <p class="li"><span>图片:</span>
                <img class="t2" src="">
                <input type="file" name="" id="imgTest" accept=".gif,.jpg,.jpeg,.png">
            </p>
            <p class="li"><span>标题：</span><input class="t3 sol" type="text" name=""></p>
            <p class="li"><span>链接：</span><input type="text" class="t4 log" name=""></p>
            <p class="li"><span>内容：</span><input type="text" id="date_start" class="t5 sol" name=""></p>
            <p><button class="addClick">确认</button></p>
        </div>
    </div>
</body>
<script src="../../js/jquery.js"></script>
<script src="../../js/jquery.cookie.js"></script>
<script type="text/javascript">
    $(function(){
        
        getData();
        function getData(){
            var admintoken=$.cookie('admintoken');
            if(admintoken!=null){
                $('.loading').show();
                $.ajax({
                    type: 'get',
                    url:'http://www.math80.com/ads',
                    headers:{Authorization:"Bearer "+admintoken},
                    success:function(data){
                        console.log(data)
                        var all=data.data;
                        var len=data.data.length;
                        $('.info ul li').not('.top').remove();
                        for(var i=0;i<len;i++){
                            var li="<li>"+
                                        "<span class='id'>"+all[i].id+"</span>"+
                                        "<span class='name'>"+all[i].ad_name+"</span>"+
                                        "<span class='logo'><img src='"+all[i].ad_logo+"'></span>"+
                                        "<span class='title'>"+all[i].ad_title+"</span>"+
                                        "<span class='url'>"+all[i].ad_url+"</span>"+
                                        "<span class='texta'>"+all[i].ad_text+"</span>"+
                                        "<span class='click'><a class='clicka'>修改</a> <a class='clickb'>删除</a></span>"+
                                    "</li>";
                            $('.info ul').append(li);
                        }
                        $('.loading').hide();

                    }
                })
            }
        }
        

        //上传图片
        $('#imgTest').change(function(e){
            var reader = new FileReader();
            reader.onload = (function (file) {
                return function (e) {
                    console.log(this.result)
                   $('.t2').attr('src',this.result);
                };
            })(e.target.files[0]);
            reader.readAsDataURL(e.target.files[0]);
        })

        //点击修改内容
        var id=null;
        var logo;
        $(document).on('click','.clicka',function(){

            id=$(this).parent().parent().find('.id').html();
            var name=$(this).parent().parent().find('.name').html();
            logo=$(this).parent().parent().find('.logo img').attr('src');
            var title=$(this).parent().parent().find('.title').html();
            var url=$(this).parent().parent().find('.url').html();
            var texta=$(this).parent().parent().find('.texta').html();

            $('.t1').val(name);
            $('.t2').attr('src',logo);
            $('.t3').val(title);
            $('.t4').val(url);
            $('.t5').val(texta);
            
        });

        //提交修改信息
        $('.addClick').click(function(){
            var admintoken=$.cookie('admintoken');
            if(admintoken!=null&&id!=null){
                var n_name=$('.t1').val();
                var n_logo=$('.t2').attr('src');
                var n_title=$('.t3').val();
                var n_url=$('.t4').val();
                var n_text=$('.t5').val();

                if(n_logo==logo){
                    var data={
                        "ad_position":1,
                        "ad_name":n_name,
                        "ad_title":n_title,
                        "ad_url":n_url,
                        "ad_text":n_text
                    };
                }else{
                    var data={
                        "ad_position":1,
                        "ad_name":n_name,
                        "ad_title":n_title,
                        "ad_url":n_url,
                        "ad_text":n_text,
                        "ad_logo":n_logo
                    };
                }
                
                $('.loading').show();
                $.ajax({
                    type: 'put',
                    url:'http://www.math80.com/ads/'+id,
                    headers:{Authorization:"Bearer "+admintoken},
                    data:data,
                    success:function(data){
                        alert('修改成功!');
                        $('.loading').hide();
                        $('input').val('')
                        $('.nicEdit-main').html('');
                        $('.t2').attr('src','');
                        getData();
                    }
                })
            }else{
                alert('请重新登录')
            }
        })
        


    })

</script>
<style type="text/css">
.info .logo img{display: block !important;}
</style>
</html>